/*
 * $Id: directinputwrapper.cpp,v 1.1 2006/05/20 07:20:36 jdfekte Exp $
 *
 * Copyright (c) 2001-2002 Pierre Dragicevic, Jean-Daniel Fekete and
 * Ecole des Mines de Nantes.  All rights reserved.
 *
 * This software is proprietary information of Pierre Dragicevic, 
 * Jean-Daniel Fekete Ecole des Mines de Nantes.  You shall use it
 * only in accordance with the terms of the license agreement you
 * accepted when downloading this software.  The license is available
 * in the file licence.txt and at the following URL:
 * http://www.emn.fr/fekete/ICon/licence.html
 */

//#include <windows.h> // windows definitions
#include <afxwin.h> // To access the application handle
#define DIRECTINPUT_VERSION 0x0300 // By default, the header file is Directx 7 compatible (see dinput.h)
#include <dinput.h> // direct input
#include <jni.h>
#include "fr_emn_win32input_jdirectinput_JDirectInput.h"

/**
 * This is a cpp wrapper.
 * The syntax for calling JNI is different
 */

LPDIRECTINPUT di = 0;

/*
 * Class:     fr_emn_win32input_jdirectinput_JDirectInput
 * Method:    Initialize
 */
JNIEXPORT jboolean JNICALL Java_fr_emn_win32input_jdirectinput_JDirectInput_Initialize
  (JNIEnv *env, jclass cl) {
  // Already initialized
  if (di != 0)
    return true;
  // Create a direct input object and keep its reference
  DirectInputCreate(AfxGetInstanceHandle(), DIRECTINPUT_VERSION, &di, NULL);
  return (di != 0);
}


/*
 * Class:     fr_emn_win32input_jdirectinput_JDirectInput
 * Method:    InitializeMouse
 */
JNIEXPORT jint JNICALL Java_fr_emn_win32input_jdirectinput_JDirectInput_InitializeMouse
  (JNIEnv *env, jclass cl) {
  LPDIRECTINPUTDEVICE ref = NULL;
  // GUID_SysMouse: Globally Unique Identifier for the system mouse
  di->CreateDevice(GUID_SysMouse, &ref, NULL);
  return (jint)ref;
}

/*
 * Class:     fr_emn_win32input_jdirectinput_JDirectInput
 * Method:    AcquireDevice
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_fr_emn_win32input_jdirectinput_JDirectInput_AcquireDevice
  (JNIEnv *env , jclass cl, jint ref) {
  LPDIRECTINPUTDEVICE dev = (LPDIRECTINPUTDEVICE)ref;
  // The device will be accessed as a mouse (can't change if already acquired): 
  dev->SetDataFormat(&c_dfDIMouse);
  // Try to acquire the device
  return ((dev->Acquire()) == DI_OK);
}

/*
 * Class:     fr_emn_win32input_jdirectinput_JDirectInput
 * Method:    UnacquireDevice
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_fr_emn_win32input_jdirectinput_JDirectInput_UnacquireDevice
  (JNIEnv *env , jclass cl, jint ref) {
   return (((LPDIRECTINPUTDEVICE)ref)->Unacquire() == DI_OK);
}

/*
 * Class:     fr_emn_win32input_jdirectinput_JDirectInput
 * Method:    GetMouseState
 */
JNIEXPORT jintArray JNICALL Java_fr_emn_win32input_jdirectinput_JDirectInput_GetMouseState
  (JNIEnv *env, jclass cl, jint ref) {
  LPDIRECTINPUTDEVICE dev = (LPDIRECTINPUTDEVICE)ref;
  DIMOUSESTATE state;

  if (dev->GetDeviceState(sizeof(state), &state) == DI_OK) {
    jint s[] = {state.lX, state.lY, state.lZ,
		state.rgbButtons[0],
		state.rgbButtons[1],
		state.rgbButtons[2],
		state.rgbButtons[3]};
    jintArray ret = (*env).NewIntArray(7);
    (*env).SetIntArrayRegion(ret, 0, 7, s);
    return ret;
  }
  else
    return NULL;
}


